package io.zerous.engine

import android.graphics.*
import io.zerous.engine.math.*
import io.zerous.engine.utils.ZCamera

/**
 * Created by Zerous on 2018/2/3.
 */
class Graphics(framebuffer: Bitmap) {
    val paint = Paint()
    val canvas = Canvas(framebuffer)

    val width = framebuffer.width
    val height = framebuffer.height

    val defaultFontSize = width / 32f
    var fontSize = width / 32f

    init {

    }

    fun clear(color: Int) = canvas.drawColor(color)

    fun drawLineWithZCamera(x: Float, y: Float, x1: Float, y1: Float, color: Int = Color.BLACK, camera: ZCamera) {
        drawLine(width/2f - camera.position.x + x, height/2f - camera.position.y + y, width/2f - camera.position.x + x1, height/2f - camera.position.y + y1, color)
    }

    fun drawLineWithZCamera(start: Vector2, vec: Vector2, color: Int = Color.BLACK, camera: ZCamera) {
        drawLineWithZCamera(start.x, start.y, start.x + vec.x, start.y + vec.y, color, camera)
    }

    fun drawLine(x: Float, y: Float, x1: Float, y1: Float, color: Int = Color.BLACK) {
        paint.color = color
        canvas.drawLine(x, y, x1, y1, paint)
    }

    fun drawText(text: String, point: Vector2, color: Int = Color.BLACK, align: TextAlign = TextAlign.CENTER, fontSize: Float = defaultFontSize) {
        drawText(text, point.x, point.y, color, align, fontSize)
    }

    fun drawText(text: String, x: Float, y: Float, color: Int = Color.BLACK, align: TextAlign = TextAlign.CENTER, fontSize: Float = defaultFontSize) {
        paint.textSize = fontSize
        paint.color = color
        paint.textAlign = when(align) {
            TextAlign.CENTER -> Paint.Align.CENTER
            TextAlign.LEFT -> Paint.Align.LEFT
            else -> Paint.Align.RIGHT
        }
        paint.typeface = Typeface.MONOSPACE
        paint.isFilterBitmap = true
        canvas.drawText(text, x, y, paint)
        paint.reset()
    }

    fun drawBitmap(bitmap: Bitmap, x: Float, y: Float) {
        canvas.drawBitmap(bitmap, x, y, paint)
    }

    fun drawCircle(x: Float, y: Float, radius: Float, color: Int = Color.BLACK, alpha: Int = 0xff) {
        paint.color = color
        paint.alpha = alpha
        canvas.drawCircle(x, y, radius, paint)
        paint.reset()
    }

    fun drawCircle(circle: Circle, color: Int = Color.BLACK, alpha: Int = 0xff) {
        drawCircle(circle.position.x, circle.position.y, circle.radius, color, alpha)
    }

    fun drawRect(rect: Rectangle, color: Int = Color.BLACK) {
        drawRect(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height, color)
    }

    fun drawRectW(x: Float, y: Float, width: Float, height: Float, color: Int = Color.BLACK) {
        drawRect(x, y, x + width, y + height, color)
    }

    fun drawRect(x: Float, y: Float, x1: Float, y1: Float, color: Int = Color.BLACK) {
        paint.color = color
        canvas.drawRect(x, y1, x1, y, paint)
        paint.reset()
    }

    fun cilp(x: Float, y: Float, width: Float, height: Float, drawCmd: () -> Unit) {
        canvas.save()
        canvas.clipRect(x, y, x+width, y+height)
        drawCmd()
        canvas.restore()
    }
}